<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.shipping.php 259 2013-09-23 08:05:24Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 259 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 08:05:24 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class shipping {
    var $modules;

	function __construct($module = '') {
		global $PHP_SELF, $order;

		if (defined('MODULE_SHIPPING_INSTALLED') && not_null(MODULE_SHIPPING_INSTALLED)) {
			$this->modules = explode(';', MODULE_SHIPPING_INSTALLED);

			$include_modules = array();

			if((not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) {
				$include_modules[] = array(	'class' => substr($module['id'], 0, strpos($module['id'], '_')),
											'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)));

			} elseif(in_array('productsshipping.php' , $this->modules)) {
				$include_modules[] = array('class' => 'productsshipping', 'file' => 'productsshipping.php');

			} else {
				reset($this->modules);
				while (list(, $value) = each($this->modules)) {
					$class = substr($value, 0, strrpos($value, '.'));
					$include_modules[] = array('class' => $class, 'file' => $value);
				}
			}

			$unallowed_modules = explode(',',$_SESSION['customers_status']['customers_status_shipping_unallowed'].','.$order->customer['shipping_unallowed']);
			for ($i = 0, $n = sizeof($include_modules); $i < $n; $i++) {
				if (in_array(str_replace('.php', '', $include_modules[$i]['file']), $unallowed_modules) != 'false') {
					// check if zone is alowed to see module
					if (constant(MODULE_SHIPPING_ . strtoupper(str_replace('.php', '', $include_modules[$i]['file'])) . _ALLOWED) != '')
						$unallowed_zones = explode(',', constant(MODULE_SHIPPING_ . strtoupper(str_replace('.php', '', $include_modules[$i]['file'])) . _ALLOWED));
					else
						$unallowed_zones = array();

					if (in_array($_SESSION['delivery_zone'], $unallowed_zones) == true || count($unallowed_zones) == 0) {
						include_once(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/shipping/' . $include_modules[$i]['file']);
						include_once(DIR_WS_MODULES . 'shipping/' . $include_modules[$i]['file']);

						$GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
					}
				}
			}
		}
	}

    function quote($method = '', $module = '') {
		global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;

		$quotes_array = array();

		if(is_array($this->modules)) {
			$shipping_quoted = '';
			$shipping_num_boxes = 1;
			$shipping_weight = $total_weight;

			if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100)
				$shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT;
			else
				$shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100);

			if ($shipping_weight > SHIPPING_MAX_WEIGHT) {
				$shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT);
				$shipping_weight = $shipping_weight/$shipping_num_boxes;
			}

			$include_quotes = array();
			if(in_array('productsshipping.php', $this->modules))
				$include_quotes[] = 'productsshipping';
			else {
				reset($this->modules);
				while(list(, $value) = each($this->modules)) {
					$class = substr($value, 0, strrpos($value, '.'));
					if(not_null($module)) {
						if(($module == $class) && ($GLOBALS[$class]->enabled) )
							$include_quotes[] = $class;

					} elseif ($GLOBALS[$class]->enabled)
						$include_quotes[] = $class;
				}
			}
			
			$size = sizeof($include_quotes);
			for($i=0; $i<$size; $i++) {
				$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
				if(!isset($quotes['error']))
					if(is_array($quotes))
						$quotes_array[] = $quotes;
			}
		}
		return $quotes_array;
    }
    
    function quote_with_reset($method = '', $module = '') {
		global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;

		$quotes_array = array();

		if(is_array($this->modules)) {
			$shipping_quoted = '';
			$shipping_num_boxes = 1;
			$shipping_weight = $total_weight;

			if(SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100)
				$shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT;
			else
				$shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100);

			if($shipping_weight > SHIPPING_MAX_WEIGHT) {
				$shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT);
				$shipping_weight = $shipping_weight/$shipping_num_boxes;
			}

			$include_quotes = array();
			if(in_array('productsshipping.php', $this->modules))
				$include_quotes[] = 'productsshipping';
			else {
				reset($this->modules);
				while(list(, $value) = each($this->modules)) {
					$class = substr($value, 0, strrpos($value, '.'));
					if(not_null($module)) {
						if(($module == $class) && ($GLOBALS[$class]->enabled) )
							$include_quotes[] = $class;

					} else
						$include_quotes[] = $class;
				}
			}
			$size = sizeof($include_quotes);
			for($i=0; $i<$size; $i++) {
				$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
				if(!isset($quotes['error']))
					if(is_array($quotes))
						$quotes_array[] = $quotes;
			}
		}
		return $quotes_array;
    }
    
    function cheapest() {
		if (is_array($this->modules)) {
			$rates = array();

			reset($this->modules);
			while (list(, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if ($GLOBALS[$class]->enabled) {
					$quotes = $GLOBALS[$class]->quotes;
					$size = sizeof($quotes['methods']);
					for ($i=0; $i<$size; $i++) {
						if(array_key_exists("cost", $quotes['methods'][$i]) && !isset ($quotes['error'][$i])) {
							$rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
											'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
											'cost' => $quotes['methods'][$i]['cost']);
						}
					}
				}
			}

			$cheapest = false;
			$size = sizeof($rates);
			for ($i=0; $i<$size; $i++) {
				if (is_array($cheapest)) {
					if ($rates[$i]['cost'] < $cheapest['cost'])
						$cheapest = $rates[$i];
				} else
					$cheapest = $rates[$i];
			}
			return $cheapest;
		}
    }
}
?>